##########################################################################################

library(dplyr)
library(ggplot2)
library(data.table)
library(RColorBrewer)
library(optparse)

##########################################################################################

option_list <- list(
    make_option(c("--info_file"), type = "character") ,
    make_option(c("--ccf_file"), type = "character") ,
    make_option(c("--images_path"), type = "character")
)

if(1!=1){

  info_file <- "~/20220915_gastric_multiple/dna_combinePublic/baseTable/STAD_Info.addBurden.MSI_MSS.addCNVType.tsv"
  ccf_file <- "~/20220915_gastric_multiple/dna_combinePublic/mutationTime/result/All_CCF_mutTime.addShare.rmMIX.tsv"

}

###########################################################################################

parseobj <- OptionParser(option_list=option_list, usage = "usage: Rscript %prog [options]")
opt <- parse_args(parseobj)
print(opt)

info_file <- opt$info_file
ccf_file <- opt$ccf_file
images_path <- opt$images_path

dir.create(images_path , recursive = T)

###########################################################################################

col <- c("#ecac54","#1e69b4")
names(col) <- c("Ever" , "Never")

###########################################################################################

dat_info <- data.frame(fread(info_file))
dat_ccf <- data.frame(fread(ccf_file))

###########################################################################################

dat_info <- subset( dat_info , Type != "IM + IGC + DGC" & TCGA_Class != "MSI" )

###########################################################################################

Variant_Type <- c("Missense_Mutation","Nonsense_Mutation","Frame_Shift_Ins","Frame_Shift_Del","In_Frame_Ins","In_Frame_Del","Splice_Site","Nonstop_Mutation")

###########################################################################################

dat_ccf <- subset( dat_ccf , Hugo_Symbol == "MUC6" & Variant_Classification %in% Variant_Type & Class == "IM" )

###########################################################################################
## 提取reccurent变异样本
recurrent_sample <- unique(subset( dat_ccf , Start_Position %in% c(1016004 , 1016412) )$Sample)
recurrent_patient <- unique(subset(dat_info , Tumor %in% recurrent_sample)$Patient)

## 提取非reccurent变异样本
nonrecurrent_sample <- unique(subset( dat_ccf , !Start_Position %in% c(1016004 , 1016412) )$Sample)
nonrecurrent_patient <- unique(subset(dat_info , Tumor %in% nonrecurrent_sample)$Patient)
nonrecurrent_patient <- nonrecurrent_patient[!nonrecurrent_patient %in% recurrent_patient]

###########################################################################################
## 标记样本的MUC6突变类型
dat_info$muc6_muttype <- ""
dat_info$muc6_muttype <- ifelse( dat_info$Patient %in% recurrent_patient , "S2130del_S2266del" , dat_info$muc6_muttype )
dat_info$muc6_muttype <- ifelse( dat_info$Patient %in% nonrecurrent_patient , "Other" , dat_info$muc6_muttype )
dat_info_use <- unique(dat_info[,c("Patient" , "Alcohol" , "muc6_muttype")])

## 计算突变率
#dat_info_use_all <- dat_info_use
#dat_info_use$Alcohol <- "All"
#dat_info_use2 <- rbind( dat_info_use , dat_info_use_all )

dat_info_use2 <- dat_info_use %>%
group_by(muc6_muttype , Alcohol ) %>%
summarize( count = n() ) %>%
mutate( ratio = count/sum(count) , count_all = sum(count) )
dat_plot <- data.frame(subset(dat_info_use2 , muc6_muttype!=""))
p <- fisher.test(matrix(dat_plot$count , 2))$p.value
dat_plot$p <- ""
dat_plot$p[1] <- p
trans <- function(num){
  up <- floor(log10(num))
  down <- round(num*10^(-up),2)
  text <- paste("P == ",down," %*% 10","^",up)
  return(text)
}
dat_plot$Alcohol <- ifelse( dat_plot$Alcohol == "Drink" , "Ever" , "Never" )
dat_plot$p <- as.numeric(dat_plot$p)
dat_plot$value_text <- round(dat_plot$ratio , 2) * 100 
dat_plot$p_text <- ifelse( dat_plot$p < 0.01 , trans(dat_plot$p) , paste0( "P == " , round(as.numeric(dat_plot$p) , 2) )  )
dat_plot$p_text <- ifelse( is.na(dat_plot$p_text) , "" , dat_plot$p_text  )
dat_plot$muc6_muttype <- paste0( dat_plot$muc6_muttype , "\n(" , dat_plot$count_all , ")" )
dat_plot$muc6_muttype <- factor( dat_plot$muc6_muttype , levels = unique(dat_plot$muc6_muttype)[2:1] )

## 画图
p <- ggplot(dat_plot , mapping = aes(muc6_muttype , ratio , fill = Alcohol )) +
  geom_bar(position = "stack", stat = "identity") + 
  theme_bw() +
  theme(strip.text.x = element_text(size = 7 , face = 'bold'))+
  theme(panel.grid=element_blank())+
  labs(y = 'Proportions (%)') +
  geom_text(aes(label=p_text , y = 1.05 ,x=1.5),size=3,parse = TRUE)+
  geom_text(aes(label=value_text) , position=position_stack(vjust =0.5) , size=4 , color="white")+
  xlab("") +
  scale_fill_manual(values=col) +
  theme(
    title =element_text(size=5, face='bold'),
    strip.text.x = element_text(size = 10, colour = "black",face='bold') ,
    axis.text.x = element_text(size = 8,color="black",face='bold') ,
    axis.text.y = element_text(size = 8,color="black",face='bold') ,
    axis.title.y = element_text(size = 10,color="black",face='bold') ,
    #axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1 , size = 8,color="black",face='bold'),
    legend.title = element_blank(),
    legend.text = element_text(size = 8),
    legend.position = "right",
    axis.title =element_text(size = 8),axis.text =element_text(size = 7, color = 'black')
  )

images_name <- paste0(images_path , "/MUC6_alcohol.recurrent.pdf")
ggsave( images_name , p , width = 3 , height = 3 )
